/*--------------------------------------------------------------------------------
	DESCRIPTION: Creating final ANES dataset for regression analysis

--------------------------------------------------------------------------------*/

clear
use "$data_dir/clean/anes_clean.dta"

//--------------------------------------------------------
//CREATE RELEVANT PROGRAMS
//--------------------------------------------------------

**CREATE TOTAL OBSERVATIONS PROGRAM
capture program drop totalobs
program define totalobs

	args var
	foreach x of varlist `var'_1-`var'_`=scalar(age)' {
		gen d_`x' = 1 if `x'!=.
	}

	cap drop total_`var'_obs*
	egen total_`var'_obs = rowtotal(d_`var'_1-d_`var'_`=scalar(age)'), missing

	drop d_`var'*

end


**CREATE MALMENDIER-NAGEL PROGRAM FROM BIRTH
capture program drop lifeexp
program define lifeexp
	
	args var
	forvalues i = 1/`=scalar(age)' {
	gen weight_`i' = `i' if age>`i' & age!=-99
	}

	forvalues i = 1/`=scalar(age)' {
	gen weighted_`i' = weight_`i'*`var'_`i'
	}
	
	egen sum_weights_1 = rowtotal(weight_1-weight_`=scalar(age)'), missing
	egen w`var'_1 = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
	replace w`var'_1 = w`var'_1/sum_weights_1

	cap drop weighted_* weight_* sum_weights_1

end


**CREATE MALMENDIER-NAGEL PROGRAM FROM BIRTH WITH EXPONENT LAMBDA
capture program drop lifeexp_lambda
program define lifeexp_lambda
	
	args var lambda
	forvalues i = 1/`=scalar(age)' {
	gen weight_`i' = (`i')^`lambda' if age>`i' & age!=-99
	}

	forvalues i = 1/`=scalar(age)' {
	gen weighted_`i' = weight_`i'*`var'_`i'
	}
	
	egen sum_weights_1 = rowtotal(weight_1-weight_`=scalar(age)'), missing
	
	if `lambda'<0 {
		
		if mod(`lambda', 1)==0 {
			local l = -1*`lambda'
			egen w`var'_neg`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_neg`l' = w`var'_neg`l'/sum_weights_1
		}
		else {
			local lambda = -1*`lambda'
			local l = round((1-(ceil(`lambda')-`lambda'))*10)
			local k = floor(`lambda')
			egen w`var'_neg`k'_`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_neg`k'_`l' = w`var'_neg`k'_`l'/sum_weights_1
		}
	} 
	else {
		if mod(`lambda', 1)==0 {
			egen w`var'_`lambda' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_`lambda' = w`var'_`lambda'/sum_weights_1
		}
		else {
			local l = round((1-(ceil(`lambda')-`lambda'))*10)
			local k = floor(`lambda')
			egen w`var'_`k'_`l' = rowtotal(weighted_1-weighted_`=scalar(age)'), missing
			replace w`var'_`k'_`l' = w`var'_`k'_`l'/sum_weights_1
		}
	}
	cap drop weighted_* weight_* sum_weights_1
end


**CREATE FORMATIVE YEARS PROGRAM
capture program drop formativeyears
program define formativeyears 

	args var
	cap drop fy_`var' pre_fy_`var' post_fy_`var'
	
	egen fy_`var' = rowmean(`var'_18-`var'_25)
	replace fy_`var' = . if `var'_25==. //make missing if haven't even turned 25 yet
	
	cap drop pre_fy_`var'
	egen pre_fy_`var' = rowmean(`var'_1-`var'_17)
	replace pre_fy_`var' = . if `var'_17==. //make missing if haven't even turned 17 yet
	
	cap drop post_fy_`var'
	egen post_fy_`var' = rowmean(`var'_26-`var'_`=scalar(age)')

end


**CREATE PRE-BIRTH PLACEBO PROGRAM
capture program drop prebirth 
program define prebirth 

	args var
	foreach i in 5 10 15 {
		egen preyob_`var'_`i' = rowmean(py_`var'_1-py_`var'_`i')
	}

end

**CREATE STANDARD DEVIATION PROGRAM
capture program drop sd
program define sd

	args var
	egen sd_`var' = rowsd(`var'_1-`var'_`=scalar(age)')

end

**CREATE LIFETIME TOTAL PROGRAM

capture program drop total
program define total

	args var
	egen total_`var' = rowtotal(`var'_1-`var'_`=scalar(age)'), missing

end

//--------------------------------------------------------
//MERGE RELEVANT DATASETS
//--------------------------------------------------------

summ age
scalar age = r(max)
cap drop yobs*
forvalues i = 0/`=scalar(age)' {
	gen yobs_`i' = .
}

local j = 0
	while `j' <= `=scalar(age)' {
		replace yobs_`j'=yob+`j' if yob!=-99
		local j = `j' + 1
}

local varlist yobs_0 yobs_1 yobs_2 yobs_3 yobs_4 yobs_5 yobs_6 yobs_7 yobs_8 yobs_9 yobs_10 yobs_11 yobs_12 yobs_13 yobs_14 yobs_15 yobs_16 yobs_17 yobs_18 yobs_19 yobs_20 yobs_21 yobs_22 yobs_23 yobs_24 yobs_25 yobs_26 yobs_27 yobs_28 yobs_29 yobs_30 yobs_31 yobs_32 yobs_33 yobs_34 yobs_35 yobs_36 yobs_37 yobs_38 yobs_39 yobs_40 yobs_41 yobs_42 yobs_43 yobs_44 yobs_45 yobs_46 yobs_47 yobs_48 yobs_49 yobs_50 yobs_51 yobs_52 yobs_53 yobs_54 yobs_55 yobs_56 yobs_57 yobs_58 yobs_59 yobs_60 yobs_61 yobs_62 yobs_63 yobs_64 yobs_65 yobs_66 yobs_67 yobs_68 yobs_69 yobs_70 yobs_71 yobs_72 yobs_73 yobs_74 yobs_75 yobs_76 yobs_77 yobs_78 yobs_79 yobs_80 yobs_81 yobs_82 yobs_83 yobs_84 yobs_85 yobs_86 yobs_87 yobs_88 yobs_89 yobs_90 yobs_91 yobs_92 yobs_93 yobs_94 yobs_95 yobs_96 yobs_97 yobs_98 yobs_99
foreach var of local varlist {
	replace `var' = . if `var'>=survey_year //so when merging, we never include the survey year itself when calculating individual experiences; everything is calculated up until the year before
}

forvalues i = 1/25 {
	gen pre_yob_`i' = .
	replace pre_yob_`i' = yob-`i' if yob!=-99
}

gen ccode = "USA"

//growth variables
local varlist growth_rate growth_shock disaster acceleration
foreach var of local varlist {
	rename survey_year year_prev
	forvalues i = 0/`=scalar(age)' {
		rename yobs_`i' year
		cap merge m:1 ccode year using "$data_dir/raw/mpd2023_clean.dta", keepusing(`var') nogen
		rename `var' `var'_`i'
		rename year yobs_`i' 
		drop if ccode!="USA"
		di "yob`i' merged"
	}
	rename year_prev survey_year
	drop if survey_year==.
}

//calculate pre-birth growth experience
rename survey_year year_prev
forvalues i = 1/25 {
	rename pre_yob_`i' year
	cap merge m:1 ccode year using "$data_dir/raw/mpd2023_clean.dta", keepusing(growth_rate) nogen
	rename growth_rate py_growth_rate_`i'
	rename year pre_yob_`i'
	drop if ccode!="USA"
	di "pre yob`i' merged"
}
rename year_prev survey_year
drop if survey_year==.

//inflation
rename survey_year year_prev
forvalues i = 0/`=scalar(age)' {
	rename yobs_`i' year
	cap merge m:1 ccode year using "$data_dir/raw/gfd_inflation_final.dta", keepusing(inflation) nogen
	replace inflation=inflation/100
	rename inflation inflation_`i'
	rename year yobs_`i' 
	drop if ccode!="USA"
	di "yob`i' merged"
}
rename year_prev survey_year
drop if survey_year==.

//financial crises
local varlist bank_crisis_w currency_crisis_w twin_crisis_w bank_crisis currency_crisis twin_crisis
foreach var of local varlist {
	rename survey_year year_prev
	forvalues i = 0/`=scalar(age)' {
		rename yobs_`i' year
		cap merge m:1 ccode year using "$data_dir/raw/financial_crises_clean.dta", keepusing(`var') nogen
		rename `var' `var'_`i'
		rename year yobs_`i' 
		drop if ccode!="USA"
		di "yob`i' merged"
	}
	rename year_prev survey_year
	drop if survey_year==.
}

//political stability
local varlist prop_unrest no_exec_changes
foreach var of local varlist {
	rename survey_year year_prev
	forvalues i = 0/`=scalar(age)' {
		rename yobs_`i' year
		cap merge m:1 ccode year using "$data_dir/raw/banks_cnts.dta", keepusing(`var') nogen
		rename `var' `var'_`i'
		rename year yobs_`i'
		drop if ccode!="USA"
		di "yob`i' merged"
	}
	rename year_prev survey_year
	drop if survey_year==.
}

//epidemic exposure
rename survey_year year_prev
forvalues i = 0/`=scalar(age)' {
	rename yobs_`i' year
	cap merge m:1 ccode year using "$data_dir/raw/natural_shocks.dta", keepusing(prop_affect_epidemic) nogen
	rename prop_affect_epidemic prop_affect_epidemic_`i'
	rename year yobs_`i'
	drop if ccode!="USA"
	di "yob`i' merged"
}
rename year_prev survey_year
drop if survey_year==.

//S&P 500 stock market return
rename survey_year year_prev
forvalues i = 0/`=scalar(age)' {
	rename yobs_`i' year
	cap merge m:1 year using "$data_dir/raw/shiller_sp500_return.dta", keepusing(sp500return) nogen
	rename sp500return sp500return_`i'
	rename year yobs_`i' 
	di "yob`i' merged"
}
rename year_prev survey_year
drop if survey_year==.

//World Inequalty Database variables
rename survey_year year_prev
forvalues i = 0/`=scalar(age)' {
	rename yobs_`i' year
	cap merge m:1 year using "$data_dir/raw/wid_usa.dta", keepusing(pretax_income_top10) nogen
	rename pretax_income_top10 pretax_income_top10_`i'
	rename year yobs_`i' 
	di "yob`i' merged"
}
rename year_prev survey_year
drop if survey_year==.

rename survey_year year_prev
forvalues i = 0/`=scalar(age)' {
	rename yobs_`i' year
	cap merge m:1 year using "$data_dir/raw/wid_usa.dta", keepusing(pretax_income_top1) nogen
	rename pretax_income_top1 pretax_income_top1_`i'
	rename year yobs_`i' 
	di "yob`i' merged"
}
rename year_prev survey_year
drop if survey_year==.

//unemployment data
rename survey_year year_prev
forvalues i = 0/`=scalar(age)' {
	rename yobs_`i' year
	cap merge m:1 year using "$data_dir/raw/us_unemployment_annual.dta", keepusing(unemployment_rate) nogen
	rename unemployment_rate unemployment_rate_`i'
	rename year yobs_`i' 
	di "yob`i' merged"
}
rename year_prev survey_year
drop if survey_year==.

cap drop yobs_*

//year of birth GDP per capita
rename yob year
merge m:1 ccode year using "$data_dir/raw/mpd2023_clean.dta", keepusing(ln_gdppc)
drop if _merge==2
drop _merge
rename year yob
rename ln_gdppc yob_ln_gdppc

//--------------------------------------------------------
//CREATE RELEVANT VARIABLES
//--------------------------------------------------------

//calculate relevant growth variables
totalobs growth_rate
formativeyears growth_rate
prebirth growth_rate

//calculate growth experiences with different lambdas
forvalues k = -3(0.1)-2 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = -2(0.1)-1 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = -1(0.1)-0.1 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = 0(0.1)1 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = 1(0.1)2 {
	lifeexp_lambda growth_rate `k'
}

forvalues k = 2(0.1)3 {
	lifeexp_lambda growth_rate `k'
}

lifeexp_lambda growth_rate 3

//other lifetime experiences
local varlist growth_shock disaster acceleration inflation sp500return pretax_income_top10 pretax_income_top1 prop_affect_epidemic bank_crisis currency_crisis twin_crisis prop_unrest no_exec_changes unemployment_rate
foreach var of local varlist {
	lifeexp `var'
}

//inflation volatility
sd inflation

drop growth_rate_*
drop growth_shock_*
drop disaster_*
drop acceleration_*
drop inflation_*
drop pretax_income_top10_*
drop pretax_income_top1_*
drop sp500return_*
drop prop_affect_epidemic_*
drop bank_crisis_*
drop currency_crisis_*
drop pre_yob_*
drop py_growth_rate_*
drop prop_unrest_*
drop no_exec_changes_*
drop unemployment_rate_*
drop twin_crisis_*

drop if survey_year==2020

save "$data_dir/clean/anes_final.dta", replace

//create cohort panel dataset
preserve
gen dummy = 1
bys survey_year cohort: egen chrt_total = total(dummy)
collapse trust_fed_govt wgrowth_rate_1 chrt_total, by(survey_year cohort)
rename wgrowth_rate_1 chrt_wgrowth_rate_1
drop if cohort==""
save "$data_dir/clean/anes_cohort_panel.dta", replace
restore
